package com.snopy.concurrency.semaphore;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

/**
 * @author snopy
 * @version 1.0.0
 * @classname DemoSemaphore
 * @description
 * @email 77912204@qq.com
 * @date 2022/06/30 11:39
 */
@Slf4j
public class DemoSemaphore {
    static Semaphore semaphore = new Semaphore(1);
    public static void main(String[] args) {
        for (int i = 0; i < 12; i++) {
            new Thread(()->{
                try{
                    log.debug("semaphore acquire...");
                    semaphore.acquire();
                    log.debug("task:{}",Thread.currentThread().getId());
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    log.debug("semaphore release...");
                    semaphore.release();
                }
            }).start();
        }
    }
}
